/*
 * @(#)ForgotPasswordAction.java 1.0 2012/03/06
 *
 * Copyright (c) 
 *
 * 
 */

package mock.ab_sd.web.action;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import mock.ab_sd.business.ForgotPasswordBOImpl;
import com.opensymphony.xwork2.ActionSupport;

/**
 * ForgotPasswordAction class 
 *  @author QuyGP
 *  @created 15/8/2012
 */
public class ForgotPasswordAction extends BaseAction{
	
	/** constant */
	private static final long serialVersionUID = 1L;
	
	/** user name */
	private String userName;
	
	/** email */
	private String email;
	
	/** store password that is returned from database */
	private String returnedPassword;
	
	/** store error message */
	private String errorMessage;
		
	/**
	 * override execute method of ActionSupport
	 * This method invokes authenticate method of ForgotPasswordBOImpl class
	 *    to authenticate user name and email, then get password from database 
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	@Override
	public String execute() throws Exception {
		
		// declare variable to store error message
		errorMessage = "";
		
		// create a ForgotPasswordBOImpl object
		ForgotPasswordBOImpl forgotPasswordBO = new ForgotPasswordBOImpl();
		
		// set returnedPassword to the returned value of authenticate method 
		setReturnedPassword(forgotPasswordBO.authenticate(userName, email));
		
		// if returned password is empty, show error message
		if (getReturnedPassword().equals("")) {
			errorMessage = "User name or email does not match database";
			return ERROR; // return the result of execution is unsuccessful
		}
		return SUCCESS; // return the result of execution is successful
	}
	
	/**
	 * override validate method of ActionSupport
	 * This method validates user name and email
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	@Override
	public void validate() {

		// if user name is empty, show error message
		if (userName.trim().length() == 0) {
			addFieldError("userName", "Please enter user name");
		}
		
		// if email is empty, show error message
		if (email.trim().length() == 0) {
			addFieldError("email", "Please enter email");
		} else {
			
			// create the right format of email
			String regEx = "^[A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]"
	                + "+([.][A-z0-9_]+)*[.][A-z]{2,4}$";
	        Pattern p = Pattern.compile(regEx);
	        Matcher m = p.matcher(email);
	        
	        // if email does not match the format, show error message
	        if (!m.find()) {
	            addFieldError("email", "email address is invalid");	
	        }
		}
	}
	
	/**
	 * get user name
	 * @return user name
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	public String getUserName() {
		return userName;
	}
	
	/**
	 * set user name
	 * @param userName
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	/**
	 * get email
	 * @return email
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	public String getEmail() {
		return email;
	}
	
	/**
	 * set email
	 * @param email
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	public void setEmail(String email) {
		this.email = email;
	}
	
	/**
	 * get password that is returned from database
	 * @return
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	public String getReturnedPassword() {
		return returnedPassword;
	}
	
	/**
	 * set returned password
	 * @param returnedPassword
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	public void setReturnedPassword(String returnedPassword) {
		this.returnedPassword = returnedPassword;
	}
	
	/**
	 * get error message
	 * @return error message
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	public String getErrorMessage() {
		return errorMessage;
	}
	
	/**
	 * set error message
	 * @param errorMessage
	 * @author QuyGP
	 * @created 15/8/2012
	 */
	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}
	
}
